//
//  MessageDao.m
//  SouthChina
//
//  Created by creator_lq on 14/12/4.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "MessageDao.h"

@implementation MessageDao


+(BOOL)insertMessage2DB:(Message*)mesg
{
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * delsql = [NSString stringWithFormat:@"DELETE from message where id='%@'",mesg.mID];
    [_dataBase executeUpdate:delsql];

    NSString *insertStr=@"INSERT INTO 'message' ('id','receiverId','sequences','user_id','senderId','messageType','mediaType','state','length','mesSesion_sess_id','ut_id','text','OTHER_INFO','UPDATE_TIME') VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    BOOL worked = [_dataBase executeUpdate:insertStr,mesg.mID,mesg.receiverId,mesg.sequences,mesg.user_id,mesg.senderId,mesg.messageType,mesg.mediaType,mesg.state,mesg.length,mesg.mesSesion_sess_id,mesg.ut_id,mesg.text,mesg.otherInfo,mesg.upate_time];
 
    [_dataBase close];
    return worked;
}


+(NSArray*)queryMessageBySessionId:(NSString *) sessinId{
    
    NSMutableArray *msgList = [[NSMutableArray alloc] init];
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * sql = @"SELECT * from message where mesSesion_sess_id=? order by UPDATE_TIME asc";
    
    FMResultSet *rs=[_dataBase executeQuery:sql,sessinId];
    Message *message = nil;
    while ([rs next]) {
        message = [[Message alloc] init];
        message.mID= [rs stringForColumn:MSG_ID];
        message.receiverId= [rs stringForColumn:RECEIVEID];
        message.sequences= [rs stringForColumn:SEQUENCES];
        message.user_id= [rs stringForColumn:USER_ID];
        message.senderId= [rs stringForColumn:SENDERID];
        message.messageType= [rs stringForColumn:MESSSAGETYPE];
        message.mediaType= [rs stringForColumn:MEDIATYPE];
        message.state= [rs stringForColumn:STATE];
        message.length= [rs stringForColumn:LENGTH];
        message.mesSesion_sess_id= [rs stringForColumn:MESSESION_ID];;
        message.ut_id= [rs stringForColumn:UT_ID];
        message.text= [rs stringForColumn:TEXT];
        message.otherInfo=[rs stringForColumn:OTHER_INFO];
        message.upate_time =[rs dateForColumn:UPDATE_TIME];
        
        [msgList addObject:message];
    };
    [rs close];
    [_dataBase close];
    return msgList;
    
}

+(Message*)queryMsgByID:(NSString *) msgId{
    
     FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * sql = @"SELECT * from message where id=? ";
    
    FMResultSet *rs=[_dataBase executeQuery:sql,msgId];
    Message *message = nil;
    while ([rs next]) {
        message = [[Message alloc] init];
        message.mID= [rs stringForColumn:MSG_ID];
        message.receiverId= [rs stringForColumn:RECEIVEID];
        message.sequences= [rs stringForColumn:SEQUENCES];
        message.user_id= [rs stringForColumn:USER_ID];
        message.senderId= [rs stringForColumn:SENDERID];
        message.messageType= [rs stringForColumn:MESSSAGETYPE];
        message.mediaType= [rs stringForColumn:MEDIATYPE];
        message.state= [rs stringForColumn:STATE];
        message.length= [rs stringForColumn:LENGTH];
        message.mesSesion_sess_id= [rs stringForColumn:MESSESION_ID];;
        message.ut_id= [rs stringForColumn:UT_ID];
        message.text= [rs stringForColumn:TEXT];
        message.otherInfo=[rs stringForColumn:OTHER_INFO];
        message.upate_time =[rs dateForColumn:UPDATE_TIME];
        
     };
    [rs close];
    [_dataBase close];
    return message;

    
}


+(BOOL) isExistMessageById:(NSString *) msgId{
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    Boolean isExist = NO;
    NSString * sql = @"SELECT * from message where id=? ";
    
    FMResultSet *rs=[_dataBase executeQuery:sql,msgId];
     while ([rs next]) {
        isExist = YES;
     };
    [rs close];
    [_dataBase close];
    return isExist;
  }

+(BOOL)deleteMessageBysessionId:(NSString*)msgsessionid
{
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * delsql = [NSString stringWithFormat:@"DELETE from message where mesSesion_sess_id='%@'",msgsessionid];
    [_dataBase executeUpdate:delsql];
    [_dataBase close];
    return NO;
}

+(BOOL)deleteMessageById:(NSString*)msgid{
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * delsql = [NSString stringWithFormat:@"DELETE from message where id='%@'",msgid];
    [_dataBase executeUpdate:delsql];
    [_dataBase close];
    return NO;
}

+(BOOL)updataMessageAddOtherInfo:(Message*) mesg addInfo:(NSDictionary*)other time:(NSDate*)time
{
    
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * delsql = [NSString stringWithFormat:@"DELETE from message where id='%@'",mesg.mID];
    [_dataBase executeUpdate:delsql];

    [mesg putValues:other];
    mesg.upate_time = time;
    
    NSString *insertStr=@"INSERT INTO 'message' ('id','receiverId','sequences','user_id','senderId','messageType','mediaType','state','length','mesSesion_sess_id','ut_id','text','OTHER_INFO','UPDATE_TIME') VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    BOOL worked = [_dataBase executeUpdate:insertStr,mesg.mID,mesg.receiverId,mesg.sequences,mesg.user_id,mesg.senderId,mesg.messageType,mesg.mediaType,mesg.state,mesg.length,mesg.mesSesion_sess_id,mesg.ut_id,mesg.text,mesg.otherInfo,mesg.upate_time];
    [_dataBase close];
    return worked;
}

+(int)queryMsgCountBySeesion:(NSString*)sessionId{
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * sql = @"SELECT count(*) as cc from message where mesSesion_sess_id=? ";
    FMResultSet *rs=[_dataBase executeQuery:sql,sessionId];
    int count = 0;
    while ([rs next]) {
        
   
        count =[rs intForColumn:@"cc"];
        
    };
    [rs close];
    [_dataBase close];
    return count;
}

+(BOOL)checkTableCreatedInDb:(FMDatabase *)db
{
    NSString *createStr=@"CREATE  TABLE  IF NOT EXISTS 'message' ('id' text PRIMARY KEY  NOT NULL  UNIQUE ,'receiverId' text,'sequences' text, 'user_id' text,'senderId' text,'messageType' text,'mediaType' text,'state' text,'length' text,'mesSesion_sess_id' text ,'ut_id' text,'text' text,'OTHER_INFO' text,'UPDATE_TIME' date)";
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    return worked;
    
}

+(BOOL)deleteMessageAll{
    
    FMDatabase *_dataBase = [DateBaseHelper openDateBase];
    [self checkTableCreatedInDb:_dataBase];
    
    NSString * delsql = [NSString stringWithFormat:@"DELETE from message"];
    BOOL worked= [_dataBase executeUpdate:delsql];
    [_dataBase close];
    return worked;
}


@end
